﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Columns;
using System.ComponentModel.Design;
using DevExpress.XtraGrid;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Base;

namespace ProgNetDxComponents.Design.Creators
{
    public partial class GridColumnRepositoryCreator : XtraForm
    {
        public GridColumnRepositoryCreator()
        {
            InitializeComponent();
        }
        private List<GridColumn> _Columns;
        private IDesignerHost _Host;
        public GridColumnRepositoryCreator(List<GridColumn> columns, IDesignerHost host)
        {
            InitializeComponent();
            _Columns = columns;
            _Host = host;
            GridColumnInfo i = null;
            foreach (GridColumn c in columns)
            {
                try
                {
                    i = new GridColumnInfo(c, host);
                    i.RepositoryCreated += new EventHandler(i_RepositoryCreated);
                    flowLayoutPanel1.Controls.Add(i);
                }
                catch { }
            }
        }

        void i_RepositoryCreated(object sender, EventArgs e)
        {
            foreach (Control c in flowLayoutPanel1.Controls)
            {
                if (c is GridColumnInfo)
                {
                    (c as GridColumnInfo).RefreshAvailableRepositories();
                }
            }
        }

        private void btnColose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnAssignAll_Click(object sender, EventArgs e)
        {
            foreach (Control c in flowLayoutPanel1.Controls)
            {
                if (c is GridColumnInfo)
                {
                    (c as GridColumnInfo).AssignRepository();
                }
            }
        }


       

        private void btnRemoveUnused_Click(object sender, EventArgs e)
        {
         List<GridControl> grids = new List<GridControl>();
            foreach (GridColumn col in _Columns)
            {
                if (!grids.Contains(col.View.GridControl))
                {
                    grids.Add(col.View.GridControl);
                }
            }
            foreach (GridControl gc in grids)
            {
                List<RepositoryItem> itemsToDelete = new List<RepositoryItem>();
                foreach (RepositoryItem item in gc.RepositoryItems)
                {
                    bool isAssigned = false;
                    foreach (BaseView bv in gc.ViewCollection)
                    {
                        if (bv is ColumnView)
                        {
                            foreach (GridColumn c in (bv as ColumnView).Columns)
                            {
                                if (c.ColumnEdit == item)
                                {
                                    isAssigned = true;
                                    break;
                                }
                            }
                        }
                        if (isAssigned)
                            break;
                    }
                    if (!isAssigned)
                        itemsToDelete.Add(item);
                }
                foreach (RepositoryItem i in itemsToDelete)
                {
                    gc.RepositoryItems.Remove(i);
                    _Host.DestroyComponent(i);
                }
            }
            foreach (Control c in flowLayoutPanel1.Controls)
            {
                if (c is GridColumnInfo)
                {
                    (c as GridColumnInfo).RefreshAvailableRepositories();
                }
            }
        }
    }
}
